package com.rm.zeyou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.rm.zeyou.entity.TechnicianShield;
import com.rm.zeyou.exception.ErrorCodeException;
import com.rm.zeyou.exception.ErrorCodeExceptionEnum;
import com.rm.zeyou.mapper.TechnicianShieldMapper;
import com.rm.zeyou.response.ApiRestResponse;
import com.rm.zeyou.service.TechnicianShieldService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rm.zeyou.util.check.CheckEmptyUtil;
import com.rm.zeyou.util.check.ValitedPhone;
import com.rm.zeyou.vo.request.AddTechnicianShieldVo;
import com.rm.zeyou.vo.response.SelectTechnicianShieldListResponseVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 技师屏蔽用户表 服务实现类
 * </p>
 *
 * @author YanYang
 * @since 2025-03-12 15:55:09
 */
@Service
public class TechnicianShieldServiceImpl extends ServiceImpl<TechnicianShieldMapper, TechnicianShield> implements TechnicianShieldService {
    @Resource
    private TechnicianShieldMapper technicianShieldMapper;

    @Override
    public ApiRestResponse<Integer> addTechnicianShield(Long tid, AddTechnicianShieldVo addVo) {
        if (!ValitedPhone.isValidPhoneNumber(addVo.getUserTel())) {
            return ApiRestResponse.error(ErrorCodeExceptionEnum.PHONE_NUMBER_ERROR);
        }
        TechnicianShield technicianShield = new TechnicianShield();
        technicianShield.setTid(tid);
        technicianShield.setUserName(addVo.getUserName());
        technicianShield.setUserTel(addVo.getUserTel());
        int insert = technicianShieldMapper.insert(technicianShield);
        return ApiRestResponse.success(insert);
    }

    @Override
    public ApiRestResponse<List<SelectTechnicianShieldListResponseVo>> selectTechnicianShieldList(Long tid) {
        List<SelectTechnicianShieldListResponseVo> voList = new ArrayList<>();
        List<TechnicianShield> technicianShields = technicianShieldMapper.selectList(new LambdaQueryWrapper<TechnicianShield>()
                .eq(TechnicianShield::getTid, tid)
                .orderByDesc(TechnicianShield::getCreateTime));
        if (!CheckEmptyUtil.isEmpty(technicianShields)) {
            technicianShields.forEach(item -> {
                SelectTechnicianShieldListResponseVo vo = new SelectTechnicianShieldListResponseVo();
                vo.setId(item.getId());
                vo.setUserName(item.getUserName());
                vo.setUserTel(item.getUserTel());
                voList.add(vo);
            });
        }

        return ApiRestResponse.success(voList);
    }

    @Override
    public ApiRestResponse<Integer> technicianDeleteShield(Long tid, Long id) {
        TechnicianShield technicianShield = technicianShieldMapper.selectById(id);
        if (CheckEmptyUtil.isEmpty(technicianShield)) {
            throw new ErrorCodeException(ErrorCodeExceptionEnum.DELETE_ERROR);
        }
        if (!technicianShield.getTid().equals(tid)) {
            throw new ErrorCodeException(ErrorCodeExceptionEnum.DELETE_ERROR);
        }
        return ApiRestResponse.success(technicianShieldMapper.deleteOne(id));
    }
}
